-- Lua Battle System - 配置验证工具
-- 验证配置文件的完整性和正确性

local Logger = require("utils.logger")
local Errors = require("utils.errors")
local TableUtils = require("utils.table_utils")

local ConfigValidator = {
    -- 配置验证规则
    rules = {
        -- 角色配置规则
        character = {
            required = {
                "id", "name", "level", 
                "max_hp", "max_mp", 
                "attack", "defense", 
                "magic_attack", "magic_defense",
                "speed", "luck"
            },
            optional = {
                "skills", "items", "equipment", "ai"
            },
            types = {
                id = "string",
                name = "string",
                level = "number",
                max_hp = "number",
                max_mp = "number",
                attack = "number",
                defense = "number",
                magic_attack = "number",
                magic_defense = "number",
                speed = "number",
                luck = "number",
                skills = "table",
                items = "table",
                equipment = "table",
                ai = "table"
            }
        },
        
        -- 技能配置规则
        skill = {
            required = {
                "id", "name", "type", 
                "cost", "range", "damage"
            },
            optional = {
                "effects", "cooldown", "description"
            },
            types = {
                id = "string",
                name = "string",
                type = "string",
                cost = "table",
                range = "string",
                damage = "table",
                effects = "table",
                cooldown = "number",
                description = "string"
            }
        },
        
        -- 物品配置规则
        item = {
            required = {
                "id", "name", "type"
            },
            optional = {
                "description", "effects", "value", "weight"
            },
            types = {
                id = "string",
                name = "string",
                type = "string",
                description = "string",
                effects = "table",
                value = "number",
                weight = "number"
            }
        },
        
        -- 战斗配置规则
        battle = {
            required = {
                "max_turns", "action_order_type"
            },
            optional = {
                "turn_time_limit", "victory_conditions", "defeat_conditions"
            },
            types = {
                max_turns = "number",
                action_order_type = "string",
                turn_time_limit = "number",
                victory_conditions = "table",
                defeat_conditions = "table"
            }
        }
    }
}

-- 配置验证构造函数
function ConfigValidator:new()
    local o = {}
    setmetatable(o, self)
    self.__index = self
    return o
end

-- 验证配置
-- @param config_type 配置类型
-- @param config 配置数据
-- @return boolean 是否验证通过
-- @return table 错误信息列表
function ConfigValidator:validate(config_type, config)
    Logger.debug("验证配置，类型: " .. config_type)
    
    local errors = {}
    local rules = self.rules[config_type]
    
    -- 检查规则是否存在
    if not rules then
        table.insert(errors, Errors:create(Errors.CONFIG_ERROR, "未知的配置类型: " .. config_type))
        return false, errors
    end
    
    -- 检查配置是否为表
    if type(config) ~= "table" then
        table.insert(errors, Errors:create(Errors.CONFIG_ERROR, "配置必须是表类型: " .. config_type))
        return false, errors
    end
    
    -- 验证必填字段
    if rules.required then
        for _, field in ipairs(rules.required) do
            if config[field] == nil then
                table.insert(errors, Errors:create(Errors.CONFIG_ERROR, string.format("必填字段 '%s' 缺失", field)))
            end
        end
    end

    -- 返回验证结果
    return #errors == 0, errors
end

return ConfigValidator